import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        list: [],
        detail: {}
    },
    mutations: {},
    actions: {
        getList(vuex) {
            if (!vuex.state.list.length) {
                axios.get("/list").then((res) => {
                    vuex.state.list = res.data
                })
            }
        },
        getDetail(vuex, obj) {
            const detail = localStorage.getItem('detail') ? JSON.stringify(localStorage.getItem('detail')) : {}
            if (detail.id == obj.id) {
                vuex.state.detail == detail
            } else {
                vuex.state.list.forEach((v) => {
                    if (v.id == obj.id) {
                        vuex.state.detail = v
                        localStorage.setItem('detail', JSON.stringify(v))
                    }
                })
            }
        }
    },
    modules: {}
})